﻿using Microsoft.Maps.MapControl.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ModernUIApp1;
using System.Collections.ObjectModel;

namespace ModernUIApp1.Pages
{
    /// <summary>
    /// Interaction logic for SplitPage1.xaml
    /// </summary>
    public partial class SplitPage1 : UserControl
    {
       
        public SplitPage1()
        {
            InitializeComponent();
            LoadRuns();
        }

        public void LoadRuns()
        {
            ObservableCollection<Track> tracks;
            tracks = new ObservableCollection<Track>();
            WcfConnection wcfConnection = new WcfConnection();
            Object id = ApplicationState.GetValue("id");

            List<Track> dbTracks = wcfConnection.getTracks((int)id);

            foreach (Track track in dbTracks)
            {
                tracks.Add(track);
            }

            tracksBinding.ItemsSource = tracks;
        }

        private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListViewItem;
            
            if (item != null)
            {
                Track track = (Track) item.Content;
                if (track.points != null && track.points.Count > 0)
                {
                    drawLines(track.points);
                }
                else
                {
                    trackMap.Children.Clear();
                }
            }
        }

        private void drawLines(List<Point> points)
        {
            trackMap.Children.Clear();


            foreach (Point point in points)
            {
                Pushpin pin = new Pushpin();
                Location location = new Location(point.posX, point.posY);
                MapLayer.SetPosition(pin, location);
                trackMap.Children.Add(pin);
            }
        }
    }
}
